package com.firebird.app.kotlin.stock.trend.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.firebird.app.kotlin.stock.trend.R
import com.firebird.app.kotlin.stock.trend.adapter.QuotationFragmentAdapter
import com.firebird.app.kotlin.stock.trend.fragment.FragmentNavigatorA
import com.firebird.app.kotlin.stock.trend.fragment.FragmentQuotationHs
import com.firebird.app.kotlin.stock.trend.view.AnimationTabWidget
import com.firebird.app.kotlin.stock.trend.view.FooterMenuView
import java.util.*

/**
 * 底部主菜单列表中显示“行情”菜单的窗口
 * @author ljk
 * @date   2019年10月27日
 */
class QuotationActivity : FragmentActivity(), OnPageChangeListener {
    private var viewPager: ViewPager? = null
    private var tabWidget: AnimationTabWidget? = null
    private var mIntent: Intent? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.setContentView(R.layout.activity_quotation)
        //初始化底部菜单控件
        val menu = findViewById<View>(R.id.footer_menu) as FooterMenuView
        menu.setCurrentMenu(R.id.menu_quotation)
        menu.setOnTouchListener(menu.MenuOnTouchListener())
        viewPager = findViewById<View>(R.id.vp_fragment) as ViewPager
        //viewPager.setAdapter(new WelcomeAdapter(getPagers()));
        viewPager!!.adapter = QuotationFragmentAdapter(supportFragmentManager, createFragments())
        viewPager!!.addOnPageChangeListener(this)
        //初始化animationTabWidget控件
        tabWidget = findViewById<View>(R.id.tab_widget) as AnimationTabWidget
        tabWidget!!.setOnClickListener(tabWidget)
        tabWidget!!.setViewPager(viewPager)
        intent = getIntent()
        viewPager!!.currentItem = intent.getIntExtra("currentItem", 0)
    }

    fun tabWidgetIsVisable(isVisable: Boolean) {
        if (isVisable) {
            tabWidget!!.visibility = View.VISIBLE
        } else {
            tabWidget!!.visibility = View.GONE
        }
    }

    private fun createFragments(): ArrayList<Fragment> {
        val list: ArrayList<Fragment> = ArrayList<Fragment>()
        val f1 = FragmentNavigatorA(this)
        val f2 = FragmentQuotationHs(this)
        list.add(f1)
        list.add(f2)
        return list
    }

    @get:SuppressLint("InflateParams")
    private val pagers: List<View>
        private get() {
            val viewList: MutableList<View> = ArrayList()
            val inflater = this.layoutInflater
            val pager1: View = inflater.inflate(R.layout.item_welcome_a, null)
            val pager2: View = inflater.inflate(R.layout.item_welcome_b, null)
            val pager3: View = inflater.inflate(R.layout.item_welcome_c, null)
            val pager4: View = inflater.inflate(R.layout.item_welcome_a, null)
            val pager5: View = inflater.inflate(R.layout.item_welcome_b, null)
            viewList.add(pager1)
            viewList.add(pager2)
            viewList.add(pager3)
            viewList.add(pager4)
            viewList.add(pager5)
            return viewList
        }

    override fun onPageScrollStateChanged(arg0: Int) {}
    override fun onPageScrolled(arg0: Int, arg1: Float, arg2: Int) {}
    override fun onPageSelected(position: Int) {
        tabWidget!!.setCurrentTab(position)
    }
}